#include "head.h"

// 单链表的创建
link_list *init_link_list(void)
{
    link_list *list = (link_list *)malloc(sizeof(link_list));
    if (NULL == list)
    {
        ERRLOG("单链表 创建失败\n");
        return NULL;
    }
    list->next = NULL;
    return list;
}

// 单链表的销毁
void free_link_list(link_list **list)
{
    if (NULL == list)
    {
        ERRLOG("单链表为空\n");
        return;
    }
    // 1 销毁结点
    Link_Node *ptr_1 = (*list)->next;
    Link_Node *ptr_2 = ptr_1->next;
    for (size_t i = 0; ptr_1 != NULL; i++)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if (NULL != ptr_2)
        {
            ptr_2 = ptr_2->next;
        }
    }
    free(*list);
    *list = NULL;
}

// 单链表的头插和中间插
void insert_link_node(link_list *list, User_data data, int pos)
{
    if (NULL == list)
    {
        ERRLOG("单链表为空\n");
        return;
    }
    Link_Node *new_node = (Link_Node *)malloc(sizeof(Link_Node));
    if (NULL == new_node)
    {
        ERRLOG("insert_link_node 创建动态空间失败\n");
    }
    new_node->data = data; // 将数据存入new_node
    new_node->next = NULL;

    // 头插
    if (1 == pos)
    {
        new_node->next = list->next;
        list->next = new_node;
        return;
    }

    // 任意位置插
    Link_Node *ptr = list->next;
    for (size_t i = 1; i < pos - 1 && ptr != NULL; i++)
    {
        ptr = ptr->next;
    }
    if (NULL == ptr)
    {
        ERRLOG("ptr已经指向NULL\n");
        free(new_node);
        return;
    }
    new_node->next = ptr->next;
    ptr->next = new_node;
    return;
}

// 单链表的尾插
void tail_link_node(link_list *list, User_data data)
{
    if (NULL == list)
    {
        ERRLOG("单链表为空\n");
        return;
    }
    Link_Node *new_node = (Link_Node *)malloc(sizeof(Link_Node));
    new_node->data = data;
    new_node->next = NULL;

    if (NULL == list->next)
    {
        list->next = new_node;
        return;
    }
    else
    {
        Link_Node *ptr = list->next;
        for (size_t i = 0; NULL != ptr->next; i++)
        {
            ptr = ptr->next;
        }
        ptr->next=new_node;
    }
}


//单链表的尾删
void tail_delete_node(link_list* list)
{
    if (NULL == list)
    {
        ERRLOG("单链表为空\n");
        return;
    }
    if (NULL == list->next)
    {
        ERRLOG("表内无数据\n");
    }
    Link_Node * ptr_1=list->next;
    Link_Node * ptr_2=ptr_1->next;
    for(size_t i=0;ptr_2->next != NULL;i++)
    {
        ptr_1=ptr_1->next;
        ptr_2=ptr_2->next;
    }
    ptr_1->next=NULL;
}

//单链表的任意删和头删
void delete_node(link_list * list,int pos)
{
    if (NULL == list)
    {
        ERRLOG("单链表为空\n");
        return;
    }
    if (NULL == list->next)
    {
        ERRLOG("表内无数据\n");
    }
    Link_Node * ptr_1=list->next;
    Link_Node * ptr_2=ptr_1->next;
    //头删
    if(1 == pos)
    {
        list->next=ptr_1->next;
        free(ptr_1);
        return ;
    }
    //任意删
    for(size_t i=1;i<pos-1 && ptr_1!=NULL;i++)
    {
        ptr_1=ptr_1->next;
    }
    if(NULL==ptr_1)
    {
        ERRLOG("ptr_1 已经指向 NULL\n");
        return ;
    }
    ptr_1->next=ptr_2->next;
    free(ptr_2);
    return ;
}



//单链表的查找(name)
Link_Node* find_node_name(link_list* list, char* name)
{
    //安全判断
    if(NULL==list)
    {
        ERRLOG("链表为空\n");
        return NULL;
    }
    if(NULL==list->next)
    {
        ERRLOG("表内无数据\n");
        return NULL;
    }
    Link_Node *ptr = list->next;
    for(size_t i=0;ptr!=NULL;i++)
    {
        if(!strcmp(ptr->data.user_name,name))
        {
            return ptr;
        }
        //向后遍历
        ptr=ptr->next;
    }
    return NULL;
}

//单链表的清空
void clear_link_list(link_list* list)
{
    //安全判断
    if(NULL == list)
    {
        printf("表 为 NULL\n");
        return ;
    }
    if(NULL == list->next)
    {
        printf("表内 无 数据\n");
        return ;
    }
    //1 销毁节点
    Link_Node * ptr_1 = list->next;
    Link_Node * ptr_2 = ptr_1->next;
    for(size_t i=0;ptr_1!=NULL;i++)
    {
        free(ptr_1);
        //if 判断  判断 ptr_2=NULL的时候 我们会如何处理
        ptr_1 = ptr_2;
        if(NULL != NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }
    //最后让list->next指向空
    list->next=NULL;
}